(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:FxproductCtrl
     * @description
     * # FxproductCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('FxproductCtrl', FxproductCtrl);

    FxproductCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', '$state'];

    function FxproductCtrl($scope, dataService, helper, $rootScope, $location, $state) {

        //列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);
        }

        $scope.getList = function(pageNo) {
            var query = {
                "content": {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize
                }
            };

            if ($scope.product_id) {
                query.content = { product_id: $scope.product_id };
            }

            dataService.fxproductGet(query).then(function(list) {
                if (list) {
                    if ($scope.product_id) {
                        $scope.product_data = list.product_list[0];
                    } else {
                        $scope.product_list = list.product_list;
                        $scope.total_count = list.total_count;
                    }
                }
            });
        }

        $scope.getGradeList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id,
                    product_id: $scope.product_data.product_id,
                    grade_id: $scope.product_data.grade_id,
                }
            };

            dataService.fxgradeGet(query).then(function(r) {
                if (r) {
                    $scope.grade_list = r.grade_list;
                    $scope.total_number = r.total_count;
                }
            });
        }

        //产品线添加页面
        $scope.addAction = function() {
            var product_id = $location.search().product_id;

            if (product_id) {
                $scope.product_id = product_id;
                $scope.getList(1);
            } else {
                var data = {
                    "grade_id": 0, //默认为0
                    "name": "产品1线",
                    // "type": 1,
                    "proportion": "10",
                    "remark": "",
                };
            }
            $scope.product_data = data;
        }

        $scope.checkGrade = function() {
            if (!$scope.product_data.grade_id) {
                helper.myAlert("error：请返回上一步设置分销商等级！");
                return false;
            }
        }

        $scope.upGrade = function() {
            var data = {
                "content": $scope.product_data,
            };
            dataService.fxproductUpdate(data).then(function(result) {
                if (result.product_id) {
                    helper.myAlert("success", "#/fxproduct/list");
                }
            });
        }

        //产品线添加
        $scope.productAdd = function(param) {
            if ($scope.product_data.name.length > 5) {
                helper.myAlert("error:产品线名称不能超过5个字");
                return false;
            }

            if ($scope.product_data.proportion > 100) {
                helper.myAlert("error:返点折扣不能大于100");
                return false;
            }

            var product_id = $location.search().product_id;
            if (product_id) {
                var data = {
                    "content": $scope.product_data,
                };
                dataService.fxproductUpdate(data).then(function(result) {
                    $scope.product_id = result.product_id;
                    // if (result.product_id) {
                    //     helper.myAlert("success");
                    // }
                });
            } else {
                var data = {
                    "content": $scope.product_data
                };

                return dataService.fxproductAdd(data).then(function(result) {
                    if (result.product_id) {
                        $scope.product_id = result.product_id;
                        $scope.product_data.product_id = result.product_id;
                    }
                    return true;
                });
            }
        }

        //点击下一步设置商品
        $scope.setItem = function() {
            // $scope.broadcast();
            $scope.checkedBroadcast();
            // $scope.huodongBroadcast();
        }

        $scope.nextAdd = function() {
            var pp = dataService.promise();

            pp = pp.then(function() {
                return $scope.productAdd();
            }).then(function() {
                $scope.setItem();
            });
        }

        //$broadcast 两个参数，1、事件名称 2、传递参数 商品列表
        $scope.broadcast = function() {
            $rootScope.$broadcast("event:productcat-item-list", { product_id: $scope.product_id });
        }

        //选中商品列表
        $scope.checkedBroadcast = function() {
            $rootScope.$broadcast("event:productcat-checked-item-list", { product_id: $scope.product_id });
        }

        //活动列表页
        $scope.huodongBroadcast = function() {
            $rootScope.$broadcast("event:productcat-huodong-list", { product_id: $scope.product_id });
        }

        $scope.itemGet1 = function(fenxiao_item_num) {
            console.log("fenxiao_item_num--->", fenxiao_item_num);
            $scope.fenxiao_item_num = fenxiao_item_num;
        }

        $scope.addGrade = function() {
            $state.go("fxgrade.add");
        }

        //tab切换
        $scope.tabCut = function($event) {
            $event.preventDefault();
            $($event.currentTarget).tab('show');
        }
    }
})();